package com.west_traveler.recyclerview_adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.west_traveler.entity.entity_find_second_hot_recommend.SecondHotRecommendDetail;
import com.west_traveler.utils.GetCirclePhoto;
import com.west_traveler.ximalayafm.R;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 * created at 2016/12/7 16:58
 *
 * @author: 黄昌友
 */

public class SecondDetailRecyclerViewAdapter extends RecyclerView.Adapter<SecondDetailRecyclerViewAdapter.ViewHolder> {
    private List<SecondHotRecommendDetail.DataBean.RecsBean.ListBean> list = new ArrayList<>();
    private SecondHotRecommendDetail.DataBean dataDetail;
    private Context context;

    public SecondDetailRecyclerViewAdapter(Context context, SecondHotRecommendDetail.DataBean dataDetail) {
        this.dataDetail = dataDetail;
        this.context = context;
        this.list=dataDetail.getRecs().getList();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(viewType, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        if (position == 0) {
            holder.tv_user_name.setText(dataDetail.getUser().getNickname());
            if (dataDetail.getUser().getFollowers() / 10000 > 0) {
                holder.tv_user_follow.setText(dataDetail.getUser().getFollowers() + "万次");
            } else {
                holder.tv_user_follow.setText(dataDetail.getUser().getFollowers() + "");
            }
            holder.tv_title_user.setText(dataDetail.getUser().getPersonalSignature());
            holder.tv_info.setText(dataDetail.getDetail().getIntro());

            new Thread() {
                @Override
                public void run() {
                    Bitmap bitmap = null;
                    try {
                        bitmap = Picasso.with(context).load(dataDetail.getUser().getSmallLogo()).get();
                        final Bitmap bitmap1 = new GetCirclePhoto().transform(bitmap);
                        holder.iv_user_icon.post(new Runnable() {
                            @Override
                            public void run() {
                                holder.iv_user_icon.setImageBitmap(bitmap1);
                            }
                        });
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
        } else {
            Picasso.with(context).load(list.get(position - 1).getCoverSmall()).into(holder.iv_recomment_icon);
            holder.tv_title_big.setText(list.get(position - 1).getTitle());
            holder.tv_title_small.setText(list.get(position - 1).getIntro());
            if (list.get(position - 1).getPlaysCounts() / 10000 > 0) {
                holder.tv_play_icon_value.setText(list.get(position - 1).getPlaysCounts() / 10000 + "万次");
            } else {
                holder.tv_play_icon_value.setText(list.get(position - 1).getPlaysCounts() + "次");
            }

            holder.tv_collect_icon_value.setText(list.get(position - 1).getTracks() + "集");
        }
    }

    @Override
    public int getItemCount() {
        return list.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return R.layout.second_detail_recyclerview_item;
        } else {
            return R.layout.second_detial_item_recycle;
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_play_icon_value;
        private TextView tv_collect_icon_value;
        private TextView tv_info;
        private TextView tv_user_name;
        private TextView tv_user_follow;
        private TextView tv_title_user;
        private TextView tv_title_big;
        private TextView tv_title_small;
        private ImageView iv_user_icon;
        private ImageView iv_recomment_icon;

        public ViewHolder(View itemView) {
            super(itemView);

            //第一个布局内容简介
            tv_info = ((TextView) itemView.findViewById(R.id.tv_info));
            tv_user_name = ((TextView) itemView.findViewById(R.id.tv_user_name));
            tv_user_follow = ((TextView) itemView.findViewById(R.id.tv_user_follow));
            tv_title_user = ((TextView) itemView.findViewById(R.id.tv_title_user));
            iv_user_icon = ((ImageView) itemView.findViewById(R.id.iv_user_icon));

            //第二个布局
            iv_recomment_icon = ((ImageView) itemView.findViewById(R.id.iv_recomment_icon));
            tv_title_big = ((TextView) itemView.findViewById(R.id.tv_title_big));
            tv_title_small = ((TextView) itemView.findViewById(R.id.tv_title_small));
            tv_play_icon_value = ((TextView) itemView.findViewById(R.id.iv_play_icon_value));
            tv_collect_icon_value = ((TextView) itemView.findViewById(R.id.iv_collect_icon_value));


        }
    }
}
